package com.dz.lm.web.controller.newuser.userproduct;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dz.lm.common.dto.UserProductDTO;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.util.FileUploadUtil;
import com.dz.lm.common.util.StringUtil;
import com.dz.lm.common.util.TecentVideo;
import com.dz.lm.common.utils.DateUtil;
import com.dz.lm.common.utils.StringUtils;
import com.dz.lm.common.utils.json.JsonUtils;
import com.dz.lm.dao.appLogin.read.AppLoginDao;
import com.dz.lm.domain.userproduct.UserProduct;
import com.dz.lm.service.attach.AttachService;
import com.dz.lm.service.pay.WxPayService;
import com.dz.lm.service.userproduct.UserProductService;
import com.dz.lm.service.usersearch.UserSearchService;
import com.dz.lm.web.controller.BasicControl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.*;

/**
 * 用户产品控制层
 *
 * @version 1.8;
 * @author: LDY;
 * @Description;
 * @Date: Created in 2020/3/11 16:32;
 */
@RestController
@RequestMapping(value = "/h5/weijie/rewriteUrl/userProduct", produces = "application/json")
public class UserProductController extends BasicControl {
    @Autowired
    private UserProductService userProductService;
    @Autowired
    private UserSearchService userSearchService;
    @Autowired
    private WxPayService wxPayService;
    @Resource(name = "readAppLoginDao")
    private AppLoginDao rAppLoginDao;
    @Autowired
    private AttachService attachService;
    /**
     * 添加产品
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/add")
    public Map add(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "操作失败";
        Object data = null;
        JSONObject subjo = new JSONObject();
        Object imgUrl = request.getAttribute("imgUrl");
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        //附件
        Map<String, Object> images = (Map<String, Object>) request.getAttribute("images");

        Map<String, Object> urlMap = new HashMap<String, Object>();
        if (images != null && !images.isEmpty()) {
            urlMap = FileUploadUtil.uploadByInStream(images, imgUrl, "product");
        }

        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        //参数校验
        Vector<String> inparam = new Vector<String>();
        for (String in : inparam) {
            if (!param.containsKey(in)) {
                subjo = new JSONObject();
                subjo.put("result", "01");
                subjo.put("message", "参数" + in + "异常");
                return subjo;
            }
        }

        //归属人
        Integer userId = param.get("userId") != null ? Integer.parseInt(param.get("userId").toString()) : 0;
        //标题
        String title = param.get("title") != null ? param.get("title").toString() : "";
        //产品内容
        String content = param.get("content") != null ? param.get("content").toString() : "";
        //视频地址(关联附件id)
        Integer vedioId = param.get("videoId") != null ? Integer.parseInt(param.get("videoId").toString()) : 0;
        //产品名称
        String projectName = param.get("productName") != null ? param.get("productName").toString() : "";
        //模板json{name:””,...}
        String template = param.get("template") != null ? param.get("template").toString() : "";
        //province	省市编码
        String province = param.get("province") != null ? param.get("province").toString() : "110100";
        //city	城市编码
        String city = param.get("city") != null ? param.get("city").toString() : "";
        //区
        String area = param.get("area") != null ? param.get("area").toString() : "";
        //返点设置
        double returnPoint = param.get("returnPoint") != null ? Double.parseDouble(param.get("returnPoint").toString()) : 0.00;
        //代理费用
        double agentCost = param.get("agentCost") != null ? Double.parseDouble(param.get("agentCost").toString()) : 0.00;
        //发放个数
        Integer grantNum = param.get("grantNum") != null ? Integer.parseInt(param.get("grantNum").toString()) : 0;
        //代理是否需要审核
        Integer isReview = param.get("isReview") != null ? Integer.parseInt(param.get("isReview").toString()) : 0;
        //申请需要审核 0 不需审核1
        Integer applyReview = param.get("applyReview") != null ? Integer.parseInt(param.get("applyReview").toString()) : 0;
        //申请通过后私信
        String applyMessage = param.get("applyMessage") != null ? param.get("applyMessage").toString() : "";
        //代理通过后私信
        String agentMessage = param.get("agentMessage") != null ? param.get("agentMessage").toString() : "";

        //代理审核模板json{name:””,...}
        String agentTemplate = param.get("agentTemplate") != null ? param.get("agentTemplate").toString() : "";
        //代理等级
        String agentLevel = param.get("agentLevel") != null ? param.get("agentLevel").toString() : "";
        //原价
        double price = param.get("price") != null ? Double.parseDouble(param.get("price").toString()) : 0.00;

        //小程序标识
        String wxcode = param.get("wxcode") != null ? param.get("wxcode").toString() : "";
        //添加产品
        UserProduct product = new UserProduct();
        if (!"100000".equals(province)) { //全国：100000
            product.setProvince(province);
            //全北京 天津 上海 重庆
            if (!"".equals(city) && !"110100".equals(city) && !"120100".equals(city) && !"310100".equals(city) && !"500100".equals(city))
                product.setCity(city);
        }
        double fee = agentCost * grantNum;
        product.setUserId(userId);
        product.setProductType(1);//1原生2代理
        product.setSuperId(0);//代理原生id,默认0
        product.setAgentStatus(0);//0原生
        product.setStatus(2);
        product.setTitle(title);
        product.setContent(content);
        product.setImageUrl1(urlMap.get("imageUrl1") != null ? imgUrl + urlMap.get("imageUrl1").toString() : "");
        product.setImageUrl2(urlMap.get("imageUrl2") != null ? imgUrl + urlMap.get("imageUrl2").toString() : "");
        product.setImageUrl3(urlMap.get("imageUrl3") != null ? imgUrl + urlMap.get("imageUrl3").toString() : "");
        product.setImageUrl4(urlMap.get("imageUrl4") != null ? imgUrl + urlMap.get("imageUrl4").toString() : "");
        product.setImageUrl5(urlMap.get("imageUrl5") != null ? imgUrl + urlMap.get("imageUrl5").toString() : "");
        product.setTemplate(template);
        product.setArea(area);
        product.setReturnPoint(returnPoint);
        product.setAgentCost(0d);
        product.setGrantNum(grantNum);
        product.setRank(0);
        product.setVideoId(vedioId);
        product.setProductName(projectName);
        product.setSource(userId);
        product.setIsReview(isReview);
        product.setAgentTemplate(agentTemplate);
        product.setApplyReview(applyReview);
        product.setApplyMessage(applyMessage);
        product.setAgentMessage(agentMessage);
        product.setAgentLevel(agentLevel);
        product.setPrice(price);
        try {
            product.setCreateTime(com.smartlbs.util.DateUtil.getNowDateTimeString());
            product.setUpdateTime(com.smartlbs.util.DateUtil.getNowDateTimeString());//发布时间
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if(returnPoint > 0 && StringUtils.getNumberDecimalDigits(returnPoint) > 1) { //判断返点格式
            message = "成交返点请设置为小数点后一位";
            result = "01";
        }else if(agentCost > 0 && StringUtils.getNumberDecimalDigits(agentCost) > 1){ //判断代理金额格式
            message = "代理小费请设置为小数点后一位";
            result = "02";
        }else {
            int add = userProductService.add(product);
            if (add > 0) {
                message = "添加成功";
                result = "00";
                Map m = new HashMap();
                m.put("productId",add);
                if (fee>0) { //统一下单
                    Map map = new HashMap();
                    map.put("userId", userId);
                    map.put("amount",fee);
                    map.put("agentCost",agentCost);
                    map.put("orderType",4);
                    map.put("productId",product.getProductId());
                    map.put("type",1);//发布产品
                    map.put("body","代理费");
                    map.put("platform", param.get("platform"));
                    if(!StringUtil.containEmpty(wxcode)){
                        map.put("wxcode", wxcode);
                    }
                    Map<String, Object> retData = wxPayService.unifiedOrder(map);
                    m.putAll(retData);
                }
                data = m;
            }
        }

        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }

    /**
     * 修改产品
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/update")
    public Map update(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "操作失败";
        Object data = null;
        JSONObject subjo = new JSONObject();
        //登录人
        String uId = request.getAttribute("userId") != null ? request.getAttribute("userId").toString() : "";
        Object imgUrl = request.getAttribute("imgUrl");
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        //附件
        Map<String, Object> images = (Map<String, Object>) request.getAttribute("images");
        Map<String, Object> urlMap = new HashMap<String, Object>();
       if (images != null && !images.isEmpty()) {
            urlMap = FileUploadUtil.uploadByInStream(images, imgUrl, "product");
        }
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        //参数校验
        Vector<String> inparam = new Vector<String>();
        inparam.add("productId");
        for (String in : inparam) {
            if (!param.containsKey(in)) {
                subjo = new JSONObject();
                subjo.put("result", "01");
                subjo.put("message", "参数" + in + "异常");
                return subjo;
            }
        }
        //产品
        Integer productId = param.get("productId") != null ? Integer.parseInt(param.get("productId").toString()) : 0;
        //标题
        String title = param.get("title") != null ? param.get("title").toString() : "";
        //产品内容
        String content = param.get("content") != null ? param.get("content").toString() : "";
        //视频地址(关联附件id)
        Integer videoId = param.get("videoId") != null ? Integer.parseInt(param.get("videoId").toString()) : 0;
        //产品名称
        String productName = param.get("productName") != null ? param.get("productName").toString() : "";
        //模板json{name:””,...}
        String template = param.get("template") != null ? param.get("template").toString() : "";
        //是否需要审批
        Integer isReview = param.get("isReview") != null ? Integer.parseInt(param.get("isReview").toString()) : 0;

        //代理申请模板
        String agentTemplate = param.get("agentTemplate") != null ? param.get("agentTemplate").toString() : "";

        //province	省市编码
        String province = param.get("province") != null ? param.get("province").toString() : "110100";
        //city	城市编码
        String city = param.get("city") != null ? param.get("city").toString() : "";
        //区
        String area = param.get("area") != null ? param.get("area").toString() : "";
        //删除视频id
        String deFileId = param.get("deFileId") != null ? param.get("deFileId").toString() : "";
        //删除附件表id
        Integer deVideoId = param.get("deVideoId") != null ? Integer.parseInt(param.get("deVideoId").toString()) : 0;

        //返点设置
        double returnPoint = param.get("returnPoint") != null ? Double.parseDouble(param.get("returnPoint").toString()) : 0;
        //代理费用
        double agentCost = param.get("agentCost") != null ? Double.parseDouble(param.get("agentCost").toString()) : 0;
        //发放个数
        Integer grantNum = param.get("grantNum") != null ? Integer.parseInt(param.get("grantNum").toString()) : 0;
        //代理排名
        Integer rank = param.get("rank") != null ? Integer.parseInt(param.get("rank").toString()) : 0;
        //删除附件地址,逗号分隔
        String deFiles = param.get("deFiles") != null ? param.get("deFiles").toString() : "";
        String imageUrl1 = param.get("imageUrl1") != null ? param.get("imageUrl1").toString() : "";
        String imageUrl2 = param.get("imageUrl2") != null ? param.get("imageUrl2").toString() : "";
        String imageUrl3 = param.get("imageUrl3") != null ? param.get("imageUrl3").toString() : "";
        String imageUrl4 = param.get("imageUrl4") != null ? param.get("imageUrl4").toString() : "";
        String imageUrl5 = param.get("imageUrl5") != null ? param.get("imageUrl5").toString() : "";
        //申请需要审核 0 不需审核1
        Integer applyReview = param.get("applyReview") != null ? Integer.parseInt(param.get("applyReview").toString()) : 0;
        //申请通过后私信
        String applyMessage = param.get("applyMessage") != null ? param.get("applyMessage").toString() : "";
        //代理通过后私信
        String agentMessage = param.get("agentMessage") != null ? param.get("agentMessage").toString() : "";
        //原价
        double price = param.get("price") != null ? Double.parseDouble(param.get("price").toString()) : 0.00;
        //修改产品
        UserProduct product = new UserProduct();
        if (!"100000".equals(province)) { //全国：100000
            product.setProvince(province);
            //全北京 天津 上海 重庆
            if (!"".equals(city) && !"110100".equals(city) && !"120100".equals(city) && !"310100".equals(city) && !"500100".equals(city)) {
                product.setCity(city);
            }
        }
        product.setProductId(productId);
        product.setVideoId(videoId);
        if (!StringUtils.isEmpty(productName)) {
            product.setProductName(productName);
        }
        if(videoId>0){
            product.setVideoId(videoId);
        }
        if (agentCost > 0) {
            product.setAgentCost(agentCost);
        }
        if (grantNum > 0) {
            product.setGrantNum(grantNum);
        }
        if(returnPoint>-1){
            product.setReturnPoint(returnPoint);
        }
        if (!StringUtils.isEmpty(title)) {
            product.setTitle(title);
        }
        if (!StringUtils.isEmpty(content)) {
            product.setContent(content);
        }
        product.setImageUrl1(imageUrl1);
        product.setImageUrl2(imageUrl2);
        product.setImageUrl3(imageUrl3);
        product.setImageUrl4(imageUrl4);
        product.setImageUrl5(imageUrl5);
        if(urlMap.get("url1") != null){
            product.setImageUrl1(urlMap.get("imageUrl1") != null ? imgUrl + urlMap.get("imageUrl1").toString() : "");
        }
        if(urlMap.get("url2") != null ){
            product.setImageUrl2(urlMap.get("imageUrl2") != null ? imgUrl + urlMap.get("imageUrl2").toString() : "");

        }
        if(urlMap.get("url3") != null ){
            product.setImageUrl3(urlMap.get("imageUrl3") != null ? imgUrl + urlMap.get("imageUrl3").toString() : "");

        }
        if(urlMap.get("url4") != null){
            product.setImageUrl4(urlMap.get("imageUrl4") != null ? imgUrl + urlMap.get("imageUrl4").toString() : "");

        }
        if(urlMap.get("url5") != null){
            product.setImageUrl5(urlMap.get("imageUrl5") != null ? imgUrl + urlMap.get("imageUrl5").toString() : "");

        }
        if (!StringUtils.isEmpty(template)) {
            product.setTemplate(template);
        }
        product.setArea(area);
        product.setIsReview(isReview);
        product.setApplyReview(applyReview);
        product.setApplyMessage(applyMessage);
        product.setAgentMessage(agentMessage);
        product.setAgentTemplate(agentTemplate);
        product.setUpdateTime(DateUtil.getTimeStr(new Date()));
        product.setPrice(price);
        UserProductDTO dto = userProductService.getDeail(productId, 0);
        int i = userProductService.update(product);
        if(!StringUtils.isEmpty(deFileId)){ //删除视频
            TecentVideo.delete(deFileId);
            attachService.delete(deVideoId);
        }
        if (i > 0) {
            if (dto.getProductType() == 1 && dto.getReturnPoint() != returnPoint) {
                userProductService.updateAgent(Integer.parseInt(uId), productId, 0, 4, 2,returnPoint ,price);
            }
            if(!com.smartlbs.util.StringUtil.containEmpty(deFiles)){
                try {
                    for (String s : deFiles.split(",")) {
                        FileUploadUtil.deleteFile(s);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            message = "操作成功";
            result = "00";
        }
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }

    /**
     * 获取产品列表
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/list")
    public Map list(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        String uId = request.getAttribute("userId") != null ? request.getAttribute("userId").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData);

        //参数校验
        Vector<String> inparam = new Vector<String>();
        //所属人(首页用)
        Integer userId = param.get("userId") != null ? Integer.parseInt(param.get("userId").toString()) : 0;
        //原产品所属人
        Integer source = param.get("source") != null ? Integer.parseInt(param.get("source").toString()) : 0;

        //搜索内容
        String search = param.get("search") != null ? param.get("search").toString() : "";
        String desc = param.get("desc") != null ? param.get("desc").toString() : "";
        String order = param.get("order") != null ? param.get("order").toString() : "";
        String province = param.get("province") != null ? param.get("province").toString() : "";
        String city = param.get("city") != null ? param.get("city").toString() : "";
        if (!"".equals(city) && !"110100".equals(city) && !"120100".equals(city) && !"310100".equals(city) && !"500100".equals(city)) {

        } else {
            city = "";
        }
        //发布类别 -1-下架 1-未发布 2发布
        Integer status = param.get("status") != null ? Integer.parseInt(param.get("status").toString()) : 2;
        //是否可代理  1原生(可) 2代理
        Integer type = param.get("type") != null ? Integer.parseInt(param.get("type").toString()) : 0;
        Integer pageNum = param.get("page") != null ? Integer.parseInt(param.get("page").toString()) : 1;
        Integer rows = param.get("rows") != null ? Integer.parseInt(param.get("rows").toString()) : 10;
        //排序类型  0 默认收藏和发布时间 1申请数量 2 发布时间
        Integer orderType = param.get("orderType") != null ? Integer.parseInt(param.get("orderType").toString()) : 0;

        data = userProductService.query(pageNum + 1, rows, StringUtils.isEmpty(uId) ? 0 : Integer.parseInt(uId), userId, source, search, type, status, province, city, orderType);
        if (!StringUtils.isEmpty(search) && !StringUtil.containEmpty(uId)) {
            userSearchService.add(search, StringUtils.isEmpty(uId) ? 0 : Integer.parseInt(uId));
        }
        message = "获取成功";
        result = "00";
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }

    /**
     * 获取产品详情
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/get")
    public Map get(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        String uId = request.getAttribute("userId") != null ? request.getAttribute("userId").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData);
        //参数校验
        Vector<String> inparam = new Vector<String>();
        //产品
        Integer productId = param.get("productId") != null ? Integer.parseInt(param.get("productId").toString()) : 4;
        data = userProductService.getDeail(productId, StringUtil.getInt(uId,0));
        message = "获取成功";
        result = "00";
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }

    /**
     * 原生产品 发布
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/changeLowers")
    public Map changeLowers(HttpServletRequest request) throws ServiceException{
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        String uId = request.getAttribute("userId").toString();
        Map<String, Object> param = JsonUtils.readValue(reqData);
        //参数校验
        Vector<String> inparam = new Vector<String>();
        //归属人
        Integer userId = param.get("userId") != null ? Integer.parseInt(param.get("userId").toString()) : 0;
        //产品
        Integer productId = param.get("productId") != null ? Integer.parseInt(param.get("productId").toString()) : 2;
        //province	省市编码
        String province = param.get("province") != null ? param.get("province").toString() : "110100";
        //city	城市编码
        String city = param.get("city") != null ? param.get("city").toString() : "";
        //区
        String area = param.get("area") != null ? param.get("area").toString() : "";
        //代理费用
        double agentCost = param.get("agentCost") != null ? Double.parseDouble(param.get("agentCost").toString()) : 0.00;
        //发放个数
        Integer grantNum = param.get("grantNum") != null ? Integer.parseInt(param.get("grantNum").toString()) : 0;
        //小程序标识
        String wxcode = param.get("wxcode") != null ? param.get("wxcode").toString() : "";
        if(productId > 0){
            UserProduct product = new UserProduct();
            product.setProductId(productId);
            product.setAgentCost(agentCost);
            product.setGrantNum(grantNum);
            if (!"100000".equals(province)) { //全国：100000
                product.setProvince(province);
                //全北京 天津 上海 重庆
                if (!"".equals(city) && !"110100".equals(city) && !"120100".equals(city) && !"310100".equals(city) && !"500100".equals(city))
                    product.setCity(city);
            }
            double fee = agentCost * grantNum;
            if(fee ==0){
                product.setStatus(2);//代理小费为0直接发布
            }
            int update = userProductService.update(product);
            if(update > 0){
                message = "操作成功";
                result = "00";
                if(fee > 0){
                    Map m = new HashMap();
                    m.put("productId",productId);
                    if (fee>0) { //统一下单
                        Map map = new HashMap();
                        map.put("userId", userId);
                        map.put("amount",fee);
                        map.put("agentCost",agentCost);
                        map.put("orderType",4);
                        map.put("productId",product.getProductId());
                        map.put("type",1);//发布产品
                        map.put("body","代理费");
                        map.put("platform", param.get("platform"));
                        if(!StringUtil.containEmpty(wxcode)){
                            map.put("wxcode", wxcode);
                        }
                        Map<String, Object> retData = wxPayService.unifiedOrder(map);
                        m.putAll(retData);
                    }
                    data = m;
                }

            }

        }else {
            message = "参数有误";
            result = "01";
        }

        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }
    /**
     * 产品发布和下架
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/changeLowe")
    public Map changeLower(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        String uId = request.getAttribute("userId").toString();
        Map<String, Object> param = JsonUtils.readValue(reqData);
        //参数校验
        Vector<String> inparam = new Vector<String>();
        //产品
        Integer productId = param.get("productId") != null ? Integer.parseInt(param.get("productId").toString()) : 2;
        //状态 -1-下架 1-未发布 2发布 3 删除，目前只有代理产品删除走这个。
        Integer status = param.get("status") != null ? Integer.parseInt(param.get("status").toString()) : 2;
        //province	省市编码
        String province = param.get("province") != null ? param.get("province").toString() : "110100";
        //city	城市编码
        String city = param.get("city") != null ? param.get("city").toString() : "";
        UserProductDTO detail = userProductService.getDetail(0, productId, false);
        int i = 0;
        if (status == 2 && detail.getProductType() == 2) { //代理产品的发布
            synchronized (detail.getSource() + "") { //按原产品同步,计算代理费
                i = userProductService.updateLowe(productId, status ,province ,city);
            }
        } else {
            i = userProductService.updateLowe(productId, status ,province ,city);

        }
        if (i > 0) {
            switch (status){
                case -1:
                    message = "下架成功";
                    break;
                case 2:
                    message = "发布成功";
                    break;
            }
            result = "00";
        }

        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }

    /**
     * 获取受邀列表
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/sendsList")
    public Map sendsList(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        String uId = request.getAttribute("userId") != null ? request.getAttribute("userId").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData);

        //参数校验
        Vector<String> inparam = new Vector<String>();
        //受邀请人
        Integer userId = param.get("userId") != null ? Integer.parseInt(param.get("userId").toString()) : 0;
        //0-发送1接受-2-拒绝
        Integer status = param.get("status") != null ? Integer.parseInt(param.get("status").toString()) : 2;
        Integer pageNum = param.get("page") != null ? Integer.parseInt(param.get("page").toString()) : 1;
        Integer rows = param.get("rows") != null ? Integer.parseInt(param.get("rows").toString()) : 10;
        data = userProductService.querSends(pageNum + 1, rows, userId, status);
        message = "获取成功";
        result = "00";
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }

    /**
     * 删除产品
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Map delete(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "操作失败";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        String uId = request.getAttribute("userId") != null ? request.getAttribute("userId").toString() : "";

        Map<String, Object> param = JsonUtils.readValue(reqData);
        //参数校验
        Vector<String> inparam = new Vector<String>();
        //参数id
        Integer productId = param.get("productId") != null ? Integer.parseInt(param.get("productId").toString()) : 0;
        int delete = 0;

        delete = userProductService.delete(productId);

        if (delete > 0) {
            message = "操作成功";
            result = "00";
        } else if (delete == -998) {
            message = "有未处理的申请";
        }
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }


    ///用户的一些内容

    /**
     * 获取用户产品信息
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/getUserInfo")
    public Map getUserInfo(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        String uId = request.getAttribute("userId") != null ? request.getAttribute("userId").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData);
        //参数校验
        Vector<String> inparam = new Vector<String>();
        //用户id
        String userId = param.get("userId") != null ?param.get("userId").toString() : "";
        data = userProductService.getUser(userId, StringUtils.isEmpty(uId) ? 0 : Integer.parseInt(uId));
        message = "获取成功";
        result = "00";
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }
    ///用户的一些内容

    /**
     * 获取所有有效人员信息
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/getAllUser")
    public Map getAllUser(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        String uId = request.getAttribute("userId") != null ? request.getAttribute("userId").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData);
        //参数校验
        Vector<String> inparam = new Vector<String>();
        Integer pageNum = param.get("page") != null ? Integer.parseInt(param.get("page").toString()) : 1;
        Integer rows = param.get("rows") != null ? Integer.parseInt(param.get("rows").toString()) : 10;
        //排序方式，1粉丝数，2代理人数(多个逗号分隔)
        String order = request.getAttribute("order") != null ? request.getAttribute("order").toString() : "";
        data = userProductService.getAllUser(pageNum + 1, rows,StringUtil.containEmpty(uId) ?0 :StringUtil.getInt(uId,0), order);
        message = "获取成功";
        result = "00";
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }

}
